package com.filetransfer.app.ui.home

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.filetransfer.api.ShareCollection
import com.filetransfer.api.ShareCollection.ShareResult
import com.filetransfer.app.R
import com.filetransfer.app.ui.upload.ShareResultDialog
import com.filetransfer.app.ui.upload.UploadActivity
import java.util.function.Consumer

class HomeFragment : Fragment(R.layout.fragment_home) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val uploadButton: LinearLayout = view.findViewById(R.id.upload_button)
        val receiveButton: LinearLayout = view.findViewById(R.id.receive_button)
        // 上传文件按钮
        uploadButton.setOnClickListener {
            chooseFile()
        }
        // 接受文件按钮
        receiveButton.setOnClickListener {
            showReceiveFileDialog()
        }
    }

    private fun chooseFile() {
        val intent = Intent(requireContext(), UploadActivity::class.java)
        startActivity(intent)
    }

    private fun getShareInfo(
        code: String,
        callback: Consumer<ShareResult>,
        onError: Consumer<Exception?>
    ) {
        Thread {
            try {
                val info = ShareCollection.getInfoByShareCode(code)
                callback.accept(info)
            } catch (e: Exception) {
                onError.accept(e)
            }
        }.start()
    }

    private fun showReceiveFileDialog() {
        val context = context ?: return
        val activity = activity ?: return
        val inputField = EditText(context)
        inputField.hint = "请输入取件码"
        val builder = AlertDialog.Builder(context)
        builder.setTitle("请输入取件码")
            .setView(inputField)
            .setPositiveButton("确认") { dialog, _ ->
                val code = inputField.text.toString().trim()
                if (code.isEmpty()) {
                    Toast.makeText(context, "请输入取件码！", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(context, "正在获取 $code 的分享", Toast.LENGTH_SHORT).show()
                    dialog.dismiss()
                    getShareInfo(
                        code,
                        { result ->
                            activity.runOnUiThread {
                                val dialog = ShareResultDialog(context, result, {})
                                dialog.show()
                            }
                        },
                        { exception ->
                            activity.runOnUiThread {
                                Toast.makeText(context, "获取分享信息失败！", Toast.LENGTH_SHORT)
                                    .show()
                            }
                        }
                    )
                }
            }
            .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
        val alertDialog = builder.create()
        alertDialog.setOnShowListener {
            // 获取 Positive 和 Negative 按钮
            val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
            val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE)
            // 设置按钮颜色
            positiveButton.setTextColor(ContextCompat.getColor(requireContext(), R.color.green))
            negativeButton.setTextColor(ContextCompat.getColor(requireContext(), R.color.red))
        }

        alertDialog.show()
    }
}